
import { aesDecode } from '@/utils/string';
const KEY = 'swXCB15WQa==FBGC';

// {
//   订单详情接口的联系人电话，因为编辑用不到，所以这里不进行解密
//   url: new RegExp(/admin\/order\/\d+\/detail/, 'g'),
//   field: [
//     { path: 'data.clientInfo.tel' }
//   ]
// }
/**
 * 如果路径中没有数组，则直接字符串路径即可，比如 a.b.c
 * 如果有数组，则需要传 path 数组，比如要获取  { items: [ a: {b: 12} ] }，则 path: ['items', 'a.b']
 */
const SECRET_LIST = [
  {
    url: 'admin/client/info/',
    field: [
      { path: 'data.invoiceInfo.accountNum' },
      { path: 'data.invoiceInfo.invoiceAddress' },
      { path: 'data.invoiceInfo.phone' },
      { path: 'data.invoiceInfo.taxpayerNum' },
      { path: ['data.payers', 'account'] }
    ]
  },
  {
    url: 'admin/mengli/client/info/',
    field: [
      { path: 'data.invoiceInfo.accountNum' },
      { path: 'data.invoiceInfo.invoiceAddress' },
      { path: 'data.invoiceInfo.phone' },
      { path: 'data.invoiceInfo.taxpayerNum' }
    ]
  },
  {
    url: 'admin/supplier/manage/info/',
    field: [{ path: 'data.phone' }]
  },
  {
    url: 'admin/commerce/info/',
    field: [{ path: 'data.contactTel' }]
  },
  {
    url: 'admin/signUp/detail/',
    field: [
      { path: 'data.address' },
      { path: 'data.phone' },
      { path: 'data.wxCode' },
      { path: ['data.accountsReceivable.list', 'identityNumber'] },
      { path: ['data.accountsReceivable.list', 'payNum'] },
      { path: ['data.accountsReceivable.list', 'tel'] }
    ]
  },
  {
    url: 'admin/issue/invoice/detail/',
    field: [
      { path: 'data.accountNum' },
      { path: 'data.issueAddress' },
      { path: 'data.taxpayerNum' },
      { path: 'data.phone' }
    ]
  },
  {
    url: 'admin/supplier/currentWithdraw/info/',
    field: [
      { path: ['data.collectMoneyData.list', 'payNum'] }
    ]
  },
  {
    url: 'admin/reimbursement/detail/',
    field: [
      { path: ['data.accountInfo', 'payNum'] }
    ]
  },
  {
    url: 'admin/loan/detail/',
    field: [
      { path: ['data.accountInfo', 'payNum'] }
    ]
  },
  {
    url: 'admin/contract/detail/',
    field: [
      { path: 'data.contractInfo.accountNumber' },
      { path: 'data.contractInfo.accountName' },
      { path: 'data.contractInfo.contactPhone' },
      { path: 'data.contractInfo.supplierAddress' }
    ]
  }
];
function findSecretUrl(url) {
  if (!url) return null;
  return SECRET_LIST.find(i => {
    if (i.url instanceof RegExp) {
      return i.url.test(url);
    } else {
      return url.indexOf(i.url) >= 0;
    }
  });
}

export const decodeField = (response) => {
  try {
    if (response.status !== 200) return;
    const config = response.config;
    const data = response.data;
    const cur = findSecretUrl(config.url);
    if (!cur || !cur.field) return;
    cur.field.forEach(i => {
      if (typeof i.path === 'string') {
        const val = aesDecode(_.get(data, i.path), KEY);
        if (val) _.set(data, i.path, val);
      } else {
        setVal(data, i.path, 0);
      }
    });
  } catch (error) {
    console.log(error);
  }
};

function setVal(data, paths, index) {
  const val = _.get(data, paths[index]);
  if (typeof val === 'string') {
    _.set(data, paths[index], aesDecode(val, KEY));
    return;
  } else if (Array.isArray(val) && val.length) {
    val.forEach(i => {
      setVal(i, paths, index + 1);
    });
  }
}
